<?php

namespace App\Services\Scenic\Query;

use App\Models\Scenic\ScenicSpotGoodsProvider;
use App\Models\Scenic\ScenicSpotProvider;
use App\Trip\Enums\Scenic\ScenicProvider;

class Base {

    protected string $provider;

    public function __construct(){}

    /**
     * 获取系统map
     * @param array $scenicIds
     * @return array
     */
    public function getDbScenicIdMap(array $scenicIds) :array
    {
        $sysScenicIdMap = [];
        if (!$scenicIds){
            return [];
        }
        $sysScenicList = ScenicSpotProvider::whereIn('provider_scenic_id',$scenicIds)
            ->where('provider', $this->provider)
            ->get();

        foreach ($sysScenicList as $item){
            $sysScenicIdMap[$item->provider_scenic_id] = $item->scenic_id;
        }
        return $sysScenicIdMap;
    }

    /**
     * 基于map获取系统id
     * @param array $sysScenicIdMap
     * @param string $scenicId
     * @return string
     */
    public function getDbScenicIdWithMap(array $sysScenicIdMap, string $scenicId) :string
    {
        if (isset($sysScenicIdMap[$scenicId])){
            $sysScenicId = $sysScenicIdMap[$scenicId];
        }else{
            $scenicSpotProvider = (new ScenicSpotProvider())->registerProviderScenic($this->provider,$scenicId);
            $sysScenicId = $scenicSpotProvider->scenic_id;
        }
        return $sysScenicId;
    }

    /**
     * 获取系统id
     * @param string $scenicId
     * @return string
     */
    public function getDbScenicId(string $scenicId) :string
    {
        $scenicSpotProvider = ScenicSpotProvider::where('provider_scenic_id',$scenicId)
            ->where('provider', $this->provider)
            ->first();

        if (!$scenicSpotProvider instanceof ScenicSpotProvider){
            $scenicSpotProvider = (new ScenicSpotProvider())->registerProviderScenic($this->provider,$scenicId);
        }

        return $scenicSpotProvider->scenic_id;
    }

    /**
     * 获取系统map
     * @param array $ticketIds
     * @return array
     */
    public function getDbTicketIdMap(array $ticketIds) :array
    {
        $sysTicketIdMap = [];
        if (!$ticketIds){
            return [];
        }
        $sysTicketList = ScenicSpotGoodsProvider::whereIn('provider_ticket_id',$ticketIds)
            ->where('provider', $this->provider)
            ->get();

        foreach ($sysTicketList as $item){
            $sysTicketIdMap[$item->provider_ticket_id] = $item->ticket_id;
        }
        return $sysTicketIdMap;
    }

    /**
     * 基于map获取系统id
     * @param array $sysTicketIdMap
     * @param string $ticketId
     * @return string
     */
    public function getDbTicketIdWithMap(array $sysTicketIdMap,string $scenicId, string $ticketId) :string
    {
        if (isset($sysTicketIdMap[$ticketId])){
            $sysTicketId = $sysTicketIdMap[$ticketId];
        }else{
            $ticketSpotProvider = (new ScenicSpotGoodsProvider())->registerProviderTicket($this->provider,$scenicId,$ticketId);
            $sysTicketId = $ticketSpotProvider->ticket_id;
        }
        return $sysTicketId;
    }

    /**
     * 获取系统id
     * @param string $ticketId
     * @return string
     */
    public function getDbTicketId(string $scenicId,string $ticketId) :string
    {
        $ticketSpotProvider = ScenicSpotGoodsProvider::where('provider_ticket_id',$ticketId)
            ->where('provider', $this->provider)
            ->first();

        if (!$ticketSpotProvider instanceof ScenicSpotGoodsProvider){
            $ticketSpotProvider = (new ScenicSpotGoodsProvider())->registerProviderTicket($this->provider,$scenicId,$ticketId);
        }

        return $ticketSpotProvider->ticket_id;
    }



}